package com.byp.java_sport.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.byp.java_sport.config.annotation.RestControllerMapping;
import com.byp.java_sport.model.PageParams;
import com.byp.java_sport.model.Result;
import com.byp.java_sport.pojo.SysUser;
import com.byp.java_sport.service.SysUserService;
import com.byp.java_sport.utils.PasswordUtils;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestControllerMapping("user")
@Tag(name = "用户管理")
@ApiSupport(author = "linnuo:2024110510@mai.hfut.edu.cn",order = 1)
@RequestMapping("/su")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;//autowired有警告

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */

    @GetMapping("{id}")
    @Operation(summary = "通过ID查询单条数据",parameters = {
            @Parameter(name = "id",description = "主键",required = true,in= ParameterIn.PATH)
    })
    public Result<SysUser> queryById(@PathVariable String id) {
        return Result.success(sysUserService.getById(id));
    }



    @PostMapping("page")
    @Operation(summary = "分页查询")
    public Result<Page<SysUser>> pageQuery(@RequestBody PageParams<SysUser> pageParams) {

        SysUser params = pageParams.getParams();
        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StrUtil.isNotBlank(params.getNickname()), SysUser::getNickname, params.getNickname());
        return Result.success(sysUserService.page(pageParams.getPage(), lqw));
    }

    /**
     * 新增数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */

    @PostMapping("insert")
    @Operation(summary = "新增数据")
    public Result<Boolean> insert(@RequestBody SysUser sysUser) {
        String slat = RandomUtil.randomString(16);
        sysUser.setSlat(slat);
        String password = PasswordUtils.encrypt(slat, sysUser.getPassword());
        sysUser.setPassword(password);
        if (sysUserService.save(sysUser)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 更新数据
     *
     * @param sysUser 实例对象
     * @return 实例对象
     */
    @PostMapping("update")
    @Operation(summary = "更新数据")
    public Result<SysUser> update(@RequestBody SysUser sysUser) {
        if (sysUserService.updateById(sysUser)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */

    @GetMapping("delete/{id}")
    @Operation(summary = "删除数据",parameters = {
            @Parameter(name = "id",description = "主键",required = true,in= ParameterIn.PATH)
    })
    public Result<Boolean> deleteById(@PathVariable String id) {
        if (sysUserService.removeById(id)) {
            return Result.success();
        }
        return Result.error();
    }
}